# REPLICATION INFO ####
# prepared for replication April 25, 2023
# R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
# macOS Ventura 13.0.1

# package ####
library(cjoint)

# read in conjoint data #####
dat <- read.qualtrics("ethics_apsa_survey.csv",
                      responses=c("task1choice", "task2choice", "task3choice", "task4choice", "task5choice"),
                      respondentID="respondent")

# RECODING ####

## author affiliation ####
table(dat$Author.affiliation)

dat$Affiliation <- NA
dat$Affiliation[dat$Author.affiliation=="Columbia University" | dat$Author.affiliation=="Princeton University" | dat$Author.affiliation=="Stanford University" | dat$Author.affiliation=="UC-San Diego" | dat$Author.affiliation=="UC-Davis" ] <- "Top 20 Research Univ."
dat$Affiliation[dat$Author.affiliation=="Amherst College" | dat$Author.affiliation=="Middlebury College" | dat$Author.affiliation=="Oberlin College"] <- "SLAC"
dat$Affiliation[dat$Author.affiliation=="Florida State University"| dat$Author.affiliation=="University of Georgia"| dat$Author.affiliation=="Boston University"| dat$Author.affiliation=="University of Kansas"| dat$Author.affiliation=="University of Connecticut"] <- "Other R1 Univ."
dat$Affiliation[dat$Author.affiliation=="Providence College" | dat$Author.affiliation=="Fairfield University" | dat$Author.affiliation=="Fordham University"| dat$Author.affiliation=="Clark University"| dat$Author.affiliation=="Missouri State University"| dat$Author.affiliation=="Western Michigan University"] <- "Unranked Research Univ."
dat$Affiliation[dat$Author.affiliation=="London School of Economics" | dat$Author.affiliation=="Aarhus University" | dat$Author.affiliation=="University of Kent"]<- "European Research Univ."

## journal ####
table(dat$Publication.outlet)

dat$Outlet <- NA
dat$Outlet[dat$Publication.outlet=="American Journal of Political Science" | dat$Publication.outlet=="American Political Science Review" | dat$Publication.outlet=="Journal of Politics"] <- "Top 3 Journal"
dat$Outlet[dat$Publication.outlet=="Political Behavior" | dat$Publication.outlet=="Comparative Political Studies" | dat$Publication.outlet=="Electoral Studies"] <- "Subfield Journal"
dat$Outlet[dat$Publication.outlet=="Political Science Review" ] <- "Political Science Review (fake)"
dat$Outlet[dat$Publication.outlet=="Science" ] <- "Science"
dat$Outlet[dat$Publication.outlet=="Political Research Quarterly" | dat$Publication.outlet=="Political Science Research and Methods"| dat$Publication.outlet=="Quarterly Journal of Political Science"| dat$Publication.outlet=="Research and Politics" | dat$Publication.outlet=="Social Science Quarterly" | dat$Publication.outlet=="The Forum"] <- "Other General Journal"
dat$Outlet[dat$Publication.outlet=="Working paper (unpublished)"] <- "Working paper (unpublished)"

## conclusions ####
dat$Conclusion <- NA
dat$Conclusion[dat$Conclusions=="Elected officials were found to be biased against minorities."] <- "Bias against minorities"
dat$Conclusion[dat$Conclusions=="Elected officials were found to be biased towards minorities and against members of the majority group."] <- "Bias against majorities"
dat$Conclusion[dat$Conclusions=="No evidence of bias by elected officials in either direction was found."] <- "No bias"

## factors, relevel baselines ####
dat$Author <- as.factor(dat$Author.rank)
dat$Location <- as.factor(dat$Location.of.study)
dat$Sample <- as.factor(dat$Sample.size)
dat$Method <- as.factor(dat$Study.type)
dat$Affiliation <- as.factor(dat$Affiliation)
dat$Outlet <- as.factor(dat$Outlet)
dat$Conclusion <- as.factor(dat$Conclusion)

dat$Author <- relevel(dat$Author, ref="Full Professor")
dat$Conclusion <- relevel(dat$Conclusion, ref="No bias")
dat$Location <- relevel(dat$Location, ref="United States")
dat$Sample <- relevel(dat$Sample, ref="200")
dat$Method <- relevel(dat$Method, ref="Observational")
dat$Affiliation <- relevel(dat$Affiliation, ref="Top 20 Research Univ.")
dat$Outlet <- relevel(dat$Outlet, ref="Top 3 Journal")

# drop unused factor levels #
dat <- droplevels(dat)

# cregg
detach(package:cjoint)
library(cregg)

# RESULTS #####

## main effects: AMCEs ####
f1 <- selected ~ Affiliation + Author + Conclusion + Location + Outlet + Sample + Method

amces <- cj(dat, f1, id=~respondent)

amces

## AMCEs plot (Figure 3) ####
amces_plot <- plot(amces, 
     feature_headers = FALSE, 
     vline=0, feature_labels=FALSE,
     size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 2L,scales = "free_y", strip.position = "top") + 
  xlab("Average Marginal Component Effect")+
  theme_minimal() +
  theme(axis.text=element_text(size=11),axis.title=element_text(size=14), strip.text = element_text(size = 11), legend.position="none") +
  scale_colour_manual(values = c("black", "black","black","black","black","black","black")) + 
  scale_shape_manual(values = c(1,2,5,7,13,10,9)) +
  aes(shape = feature)

#### export plot ####
pdf("amces.pdf", width=8, height=7)
amces_plot
dev.off()

# Appendix ####

## marginal means ####

mms <- cj(dat, f1, id=~respondent, estimate="mm")

mms

mm_plot <- plot(mms, 
                feature_headers = FALSE, 
                vline=0.5, feature_labels=TRUE,
                size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 2L,scales = "free_y", strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(axis.text=element_text(size=11),axis.title=element_text(size=14), strip.text = element_text(size = 11), legend.position="none")+
  scale_colour_manual(values = c("black", "black","black","black","black","black","black")) + 
  scale_shape_manual(values = c(1,2,5,7,13,10,9)) +
  aes(shape = feature)

mm_plot

### export mm plot ####
pdf("marginal_means.pdf", width=8, height=7)
mm_plot
dev.off()


## additional analyses ####

## individual affiliation ###

dat$Affiliation2 <- dat$Author.affiliation

f2 <- selected ~ Affiliation2 + Author + Conclusion + Location + Outlet + Sample + Method 

amces_affil <- cj(dat, f2, feature_labels = list(Affiliation2 = "Affiliation"), id=~respondent)

amces_affil

plot_affil <- plot(subset(amces_affil, feature=="Affiliation"),
                   feature_headers = FALSE, 
                   vline=0, feature_labels=FALSE,
                   size=2) +  
  ggplot2::facet_wrap(~feature,scales = "free_y", strip.position = "top") + 
  xlab("Average Marginal Component Effect")+
  theme_minimal() +
  theme(axis.text=element_text(size=10),axis.title=element_text(size=14), strip.text = element_text(size = 14), legend.position="none") +
  scale_colour_manual(values = c("black")) + 
  scale_shape_manual(values = c(1)) +
  aes(shape = feature)

plot_affil



pdf("si_affiliation.pdf", width=6, height=5)
plot_affil
dev.off()


## individual journal ####

dat$Outlet2 <- as.character(dat$Publication.outlet)

table(dat$Outlet2)
dat$Outlet2[dat$Outlet2=="Political Science Review"] <- "Political Science Review (fake)"

# it does not like the word "methods" in this level
dat$Outlet2[dat$Outlet2=="Political Science Research and Methods"] <- "Political Science Research and Meth."


dat$Outlet2 <- as.factor(dat$Outlet2)
table(dat$Outlet2)

f3 <- selected ~ Outlet2 + Affiliation + Author + Conclusion + Location + Sample + Method

amces_outlet <- cj(dat, f3, feature_labels = list(Outlet2 = "Outlet"))

amces_outlet

plot_outlet <- plot(subset(amces_outlet, feature=="Outlet"),
                   feature_headers = FALSE, 
                   vline=0, feature_labels=FALSE,
                   size=2) +  
  ggplot2::facet_wrap(~feature,scales = "free_y", strip.position = "top") + 
  xlab("Average Marginal Component Effect")+
  theme_minimal() +
  theme(axis.text=element_text(size=10),axis.title=element_text(size=14), strip.text = element_text(size = 14), legend.position="none") +
  scale_colour_manual(values = c("black")) +
  scale_shape_manual(values = c(13)) +
  aes(shape = feature)


plot_outlet



pdf("si_outlet.pdf", width=6, height=5)
plot_outlet
dev.off()

